package com.moli.iov.down.command.notice;

import com.moli.iov.base.model.dto.res.VehicleBaseResDto;
import com.moli.iov.base.remote.IVehicleBaseRemoteService;
import com.moli.iov.down.model.dto.req.VehicleCommandQueryDto;
import com.moli.iov.down.model.po.VehicleCommand;
import com.moli.iov.down.service.IVehicleCommandService;
import com.moli.iov.notify.enums.MessageTypeEnum;
import com.moli.iov.notify.model.dto.NoticeMessageCenterDto;
import com.moli.iov.notify.remote.INoticeMessageCenterRemoteService;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.third.model.dto.op.CommandNoticeDto;
import com.moli.iov.third.remote.IUserOpRemoteService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 通知其它服务
 * </p>
 *
 * @author zql
 * @since 2019/10/14 15:57
 */
@Slf4j
@Component("commandNotice")
public class CommandNotice {

  @Reference(group = "${service.group}", check = false)
  INoticeMessageCenterRemoteService noticeMessageCenterRemoteService;

  @Autowired
  private IVehicleCommandService vehicleCommandService;

  @Reference(group = "${service.group}", check = false)
  private IVehicleBaseRemoteService vehicleBaseRemoteService;

  @Reference(group = "${service.group}", check = false)
  private IUserOpRemoteService userOpRemoteService;

  @Value("${op.companyId}")
  private long opCompanyId;

  /**
   * 控车通知
   * @param vin
   * @param commandNo
   */
  @Async
  public void trigger(String vin, Long commandNo){

    VehicleCommand vehicleCommand = getVehicleCommand(vin, commandNo);
    if(vehicleCommand == null){
      log.error("控车通知 失败 控车人获取失败 vin:{}, commandNo:{}", vin, commandNo);
      return;
    }

    triggerNotice(vin, vehicleCommand.getOperateType(), vehicleCommand.getSysRealName());

    triggerOP(vin, vehicleCommand.getOperateType(), vehicleCommand.getOperateTypeName());

  }

  /**
   * 获取控车记录
   * @param vin
   * @param commandNo
   * @return
   */
  public VehicleCommand getVehicleCommand(String vin, Long commandNo){
    VehicleCommandQueryDto vehicleCommandQueryDto = new VehicleCommandQueryDto();
    vehicleCommandQueryDto.setVin(vin);
    vehicleCommandQueryDto.setCommandNo(commandNo);
    return vehicleCommandService.getVehicleCommand(vehicleCommandQueryDto);
  }

  /**
   * 控车通知 Notice服务
   * @param vin
   * @param operateType
   * @param sysRealName
   */
  public void triggerNotice(String vin, Integer operateType, String sysRealName){
    try{
      NoticeMessageCenterDto noticeMessageCenterDto = new NoticeMessageCenterDto();
      noticeMessageCenterDto.setMessageType(MessageTypeEnum.VEHICLE_CONTROL.getMessageCode());
      noticeMessageCenterDto.setMessageSource(operateType);
      noticeMessageCenterDto.setOperatorName(sysRealName);
      noticeMessageCenterDto.setVin(vin);
      noticeMessageCenterRemoteService.trigger(noticeMessageCenterDto);
    }catch(Exception e){
      log.error("控车通知 Notice服务 异常", e);
    }
  }

  /**
   * 控车通知OP
   * @param vin
   * @param operateType
   * @param operateName
   */
  public void triggerOP(String vin, Integer operateType, String operateName){
    try{
      RestResponse<VehicleBaseResDto> vehicleCompanyIdByVin = vehicleBaseRemoteService
          .getVehicleCompanyIdByVin(vin);
      if(vehicleCompanyIdByVin == null || !vehicleCompanyIdByVin.flag || vehicleCompanyIdByVin.getData() == null){
        return;
      }

      VehicleBaseResDto vehicleBaseResDto =  vehicleCompanyIdByVin.getData();
      if(opCompanyId != vehicleBaseResDto.getCompanyId()){
        return;
      }

      CommandNoticeDto commandNoticeDto = new CommandNoticeDto();
      commandNoticeDto.setVin(vin);
      commandNoticeDto.setOperateType(operateType);
      commandNoticeDto.setOperateName(operateName);
      userOpRemoteService.controlCommandNotice(commandNoticeDto);
    }catch(Exception e){
      log.error("控车通知OP 异常", e);
    }

  }
}
